package com.lin.tally.frag_record;

import android.inputmethodservice.KeyboardView;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lin.tally.R;
import com.lin.tally.adapter.TypeBaseAdapter;
import com.lin.tally.db.AccoountBean;
import com.lin.tally.db.DBManager;
import com.lin.tally.db.TypeBean;
import com.lin.tally.utils.BeiZhuDialog;
import com.lin.tally.utils.KeyBoardUtils;
import com.lin.tally.utils.SelectTimeDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 纪录页面当中的支出模块
 */
public abstract class BaseRecordFragment extends Fragment implements View.OnClickListener{

    KeyboardView keyboardView;
    EditText moneyEt;
    ImageView typeIv;
    TextView typeTv,beizhuTv,timeTv;
    GridView typeGv;
    List<TypeBean> typeList;
    TypeBaseAdapter adapter;
    AccoountBean accoountBean;//将需要插入到记账本当中的数据保存成对象的形式
    private SimpleDateFormat dateFormat;
    private String time;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        accoountBean = new AccoountBean();  //创建对象
        accoountBean.setTypename("其他");
        accoountBean.setsImageId(R.mipmap.ic_qita_fs);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_outcome, container, false);
        initView(view);
        setInitTime();
        //给GridView填充数据的方法
        loadDataToGV();
        setGVListener(); //设置GridView每一项的点击事件
        return view;
    }

    //获取当前时间，显示在timeTv上
    private void setInitTime() {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String time = sdf.format(date);
        timeTv.setText(time);
        accoountBean.setTime(time);

        Calendar calendar=Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH)+1;
        int day=calendar.get(Calendar.DAY_OF_MONTH);
        accoountBean.setYear(year);
        accoountBean.setMonty(month);
        accoountBean.setDay(day);
    }

    private void setGVListener() {
        typeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.selectPos=position;
                adapter.notifyDataSetChanged();//提示绘制发生变化
                TypeBean typeBean=typeList.get(position);
                String typename=typeBean.getTypename();
                typeTv.setText(typename);
                accoountBean.setTypename(typename);
                int simageId=typeBean.getsImageId();
                typeIv.setImageResource(simageId);
                accoountBean.setsImageId(simageId);
            }
        });
    }

    public void loadDataToGV() {
        typeList =new ArrayList<>();
        adapter = new TypeBaseAdapter(getContext(), typeList);
        typeGv.setAdapter(adapter);

    }

    private void initView(View view) {
        keyboardView = view.findViewById(R.id.frag_record_keyboard);
        moneyEt = view.findViewById(R.id.frag_record_et_money);
        typeIv = view.findViewById(R.id.frag_record_iv);
        typeGv = view.findViewById(R.id.frag_record_gv);
        typeTv = view.findViewById(R.id.frag_record_tv_type);
        beizhuTv = view.findViewById(R.id.frag_record_tv_beizhu);
        timeTv = view.findViewById(R.id.frag_record_tv_time);
        beizhuTv.setOnClickListener(this);
        timeTv.setOnClickListener(this);
        //让自定义软键盘显示出来
        KeyBoardUtils boardUtils= new KeyBoardUtils(keyboardView,moneyEt);
        boardUtils.showKeyboard();
        //设置接口，监听确定按钮是否被点击了
        boardUtils.setOnEnsureListener(new KeyBoardUtils.OnEnsureListener() {
            @Override
            public void onEnsure() {
                //获取输入的钱数
                String moneyStr=moneyEt.getText().toString();
                if (TextUtils.isEmpty(moneyStr) || moneyStr.equals("0")) {
                    getActivity().finish();
                    return;
                }
                float money=Float.parseFloat(moneyStr);
                accoountBean.setMoney(money);
                //获取纪录的信息，保存在数据库中
                saveAccountToDB();
                //返回上一级页面
                getActivity().finish();
            }

        });
    }
    /**
     * 抽象方法，让子类一定要重新这个方法
     */
    protected abstract void saveAccountToDB();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.frag_record_tv_time:
                showTimeDialog();
                break;
            case R.id.frag_record_tv_beizhu:
                showBZDialog();
                break;
        }
    }

    //弹出显示时间的对话框
    private void showTimeDialog(){
       SelectTimeDialog dialog= new SelectTimeDialog(getContext());
       dialog.show();
       //设置确定按钮被点击了的监听器
        dialog.setOnEnsureListener(new SelectTimeDialog.OnEnsureListener() {
            @Override
            public void onEnsure(String time, int year, int month, int day) {
                timeTv.setText(time);
                accoountBean.setTime(time);
                accoountBean.setYear(year);
                accoountBean.setMonty(month);
                accoountBean.setDay(day);
            }
        });
    }

    /**
     * 弹出备注对话框
     */
    public void showBZDialog(){
        BeiZhuDialog dialog=new BeiZhuDialog(getContext());
        dialog.show();//对话框显示出来
        dialog.setDialogSize();
        //设置点击确定按钮的事件
        dialog.setOnEnsureListener(new BeiZhuDialog.OnEnsureListener() {
            @Override
            public void onEnsure() {
                String msg=dialog.getEditText();
                if (!TextUtils.isEmpty(msg)) {
                    beizhuTv.setText(msg);
                    accoountBean.setBeizhu(msg);
                }
                dialog.cancel();
            }
        });
    }
}